package com.bdyc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdyc.controller.custom.MapVo;
import com.bdyc.controller.custom.MyPo;
import com.bdyc.controller.custom.PcategoryInfoPo;
import com.bdyc.controller.custom.ScategoryInfoPo;
import com.bdyc.po.DcategoryInfo;
import com.bdyc.po.WcategoryInfo;
import com.bdyc.service.spring.DcategoryInfoService;
import com.bdyc.service.spring.PcategoryInfoService;
import com.bdyc.service.spring.ScategoryInfoService;
import com.bdyc.service.spring.WcategoryInfoService;
import com.bdyc.util.AppConstant;

@Controller
@RequestMapping("/category")
public class CategoryController {
	
	@Autowired(required=true)
	@Qualifier("wcategoryInfoService")
	private WcategoryInfoService wcategoryInfoServiceImpl;
	
	@Autowired(required=true)
	@Qualifier("dcategoryInfoService")
	private DcategoryInfoService dcategoryInfoServiceImpl;
	
	@Autowired(required=true)
	@Qualifier("scategoryInfoService")
	private ScategoryInfoService scategoryInfoServiceImpl;
	
	@Autowired(required=true)
	@Qualifier("pcategoryInfoService")
	private PcategoryInfoService pcategoryInfoServiceImp;
	
	//添加CategoryInfo方法 根据flag判断 返回分页数据
	@RequestMapping(value="/findCategoryInfo.action",method=RequestMethod.POST)
	@ResponseBody//返回json数据注解
	public Map<String,Object> findCategoryInfo(MyPo po){
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> sendMap = MapVo.getSendMap(po);
		Integer rows = (Integer)sendMap.get("rows");
		Integer flag =  (Integer)sendMap.get("flag");
		try{
			if(flag == AppConstant.TASKWRITE){
				List<WcategoryInfo> infoList = wcategoryInfoServiceImpl.findCategoryInfoList(sendMap);
				Integer count = wcategoryInfoServiceImpl.findCategoryInfoListCount(sendMap);
				Integer index = count/rows;
				Integer sum = count%rows==0?index:index +1;
				map.put("sum", sum);
				map.put("count", count);
				map.put("info",infoList);
			}else if(flag == AppConstant.TASKDRAW){
				List<DcategoryInfo> infoList = dcategoryInfoServiceImpl.findCategoryInfoList(sendMap);
				Integer count = dcategoryInfoServiceImpl.findCategoryInfoListCount(sendMap);
				Integer index = count/rows;
				Integer sum = count%rows==0?index:index +1;
				map.put("sum", sum);
				map.put("count", count);
				map.put("info",infoList);
			}else if(flag == AppConstant.TASKSEND){
				List<ScategoryInfoPo> infoList = scategoryInfoServiceImpl.findCategoryInfo(sendMap);
				Integer count = scategoryInfoServiceImpl.findCategoryInfoCount(sendMap);
				Integer index = count/rows;
				Integer sum = count%rows==0?index:index +1;
				map.put("sum", sum);
				map.put("count", count);
				map.put("info",infoList);
			}else if(flag == AppConstant.TASKPICK){
				List<PcategoryInfoPo> infoList = pcategoryInfoServiceImp.findCategoryInfo(sendMap);
				Integer count = pcategoryInfoServiceImp.findCategoryInfoCount(sendMap);
				Integer index = count/rows;
				Integer sum = count%rows==0?index:index +1;
				map.put("sum", sum);
				map.put("count", count);
				map.put("info",infoList);
			}
			map.put(AppConstant.ISSUCC,AppConstant.SUCCESS);
			map.put(AppConstant.MSG,AppConstant.FINDSUCCESS);
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put(AppConstant.ISSUCC,AppConstant.FALL);
			map.put(AppConstant.MSG,AppConstant.FINDFALL );
			return map;
		}
	}
}
